const util = require("../../utils/util");
const {
    $Toast
} = require('../../iView/base/index');
var app = getApp();
Page({
    data: {
        //判断小程序的API，回调，参数，组件等是否在当前版本可用。
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
        canIUseGetUserProfile: false,
    },

    //页面加载
    onLoad: function () {
        var that = this;
        if (wx.getUserProfile) {
            that.setData({
                canIUseGetUserProfile: true
            });
        }
        //检查token是否存在
        var userInfo = util.getStorageSync("userInfo");
        if (userInfo != null) {
            app.globalData.userInfo = userInfo;
            wx.reLaunch({
                url: '/pages/home/home'
            });
            return;
        }
        wx.login({
            success: function (res) {
                if (res.code) {
                    console.log('发起登录请求');
                    wx.request({
                        url: encodeURI(app.globalData.api + '/Account/CheckUserInfoByCode'),
                        data: {
                            code: res.code
                        },
                        method: 'GET',
                        success: function (res) {
                            if (res.statusCode != 200) {
                                console.log('网络请求失败，请重新授权');
                            } else if (res.data.resultCode == 0) {
                                app.globalData.userInfo = res.data.data;
                                util.setStorageSync("userInfo", app.globalData.userInfo, 2 * 60 * 60);
                                $Toast({
                                    content: '登录成功',
                                    type: 'success'
                                });
                                var jumpTimer = setTimeout(function () {
                                    wx.reLaunch({
                                        url: '/pages/home/home'
                                    });
                                }, 700);
                            }
                        },
                        fail: function (res) {
                            console.log('请求错误' + res);
                        },
                        complete: function () {
                            console.log('发起请求结束');
                        }
                    });
                }
            },
            fail: function () {
                console.log('登录失败');
            },
        });
    },

    /**
     * 点击按钮授权登录
     * @param {any} e 
     */
    bindGetUserInfo: function (e) {
        if (e.detail.userInfo) {
            //用户按了允许授权按钮
            var that = this;
            wx.login({
                success: function (res) {
                    if (res.code) {
                        wx.request({
                            url: app.globalData.api + '/Account/GetUserOpenId',
                            data: {
                                code: res.code,
                                encryptedData: e.detail.encryptedData || '',
                                iv: e.detail.iv || '',
                                rawData: e.detail.rawData || '',
                                signature: e.detail.signature || ''
                            },
                            method: 'POST',
                            success: function (res) {
                                if (res.statusCode != 200) {
                                    $Toast({
                                        content: '请求失败请重新授权'
                                    });
                                    return;
                                }
                                if (res.data.resultCode == 0) {
                                    app.globalData.userInfo = res.data.data;
                                    util.setStorageSync("userInfo", app.globalData.userInfo, 2 * 60 * 60);
                                    wx.reLaunch({
                                        url: '/pages/home/home'
                                    });
                                }
                            },
                            fail: function () {
                                wx.showToast({
                                    title: '登录失败，退出重试',
                                    icon: 'loading',
                                    duration: 2000
                                });
                            }
                        });
                    }
                },
                fail: function () {
                    wx.showToast({
                        title: '登录失败，退出重试',
                        icon: 'loading',
                        duration: 2000
                    });
                }
            });
        } else {
            //用户按了拒绝按钮
            wx.showModal({
                title: '温馨提示',
                content: '您点击了拒绝授权，将无法获取用户标识，点击返回后将返回上一页，小程序提供的部分功能将受限。',
                showCancel: false,
                confirmText: '返回',
                success: function (res) {
                    // 用户没有授权成功，不需要改变 isHide 的值
                    if (res.confirm) {
                        console.log('用户点击了“返回”');
                        wx.navigateBack();
                    }
                }
            });
        }
    },

    /**
     * 点击按钮授权登录
     * @param {any} e 
     */
    bindGetUserProfile(e) {
        wx.getUserProfile({
            desc: '获取你的昵称、头像、地区及性别',
            success: function (res_user) {
                console.log('获取成功: ', res_user);
                if (res_user.userInfo) {
                    //用户按了允许授权按钮
                    wx.showLoading({
                        title: '数据处理中…'
                    });
                    wx.login({
                        success: function (res) {
                            if (res.code) {
                                console.log('发起请求');
                                wx.request({
                                    url: encodeURI(app.globalData.api + '/Account/GetUserProfileUserOpenId'),
                                    data: {
                                        code: res.code,
                                        encryptedData: res_user.encryptedData || '',
                                        iv: res_user.iv || '',
                                        rawData: encodeURI(res_user.rawData || ''),
                                        signature: res_user.signature || ''
                                    },
                                    method: 'POST',
                                    success: function (res) {
                                        wx.hideLoading();
                                        if (res.statusCode != 200) {
                                            $Toast({
                                                content: '网络请求失败，请重新授权'
                                            });
                                            console.log(res);
                                            return;
                                        }
                                        if (res.data.resultCode == 0) {
                                            app.globalData.userInfo = res.data.data;
                                            util.setStorageSync("userInfo", app.globalData.userInfo, 2 * 60 * 60);
                                            wx.reLaunch({
                                                url: '/pages/home/home'
                                            });
                                        }
                                    },
                                    fail: function (res) {
                                        console.log(res);
                                        wx.hideLoading();
                                        $Toast({
                                            content: '登录失败，退出重试',
                                            type: 'error'
                                        });
                                    },
                                    complete: function () {
                                        console.log('发起请求结束');
                                    }
                                });
                            } else {
                                wx.hideLoading();
                                $Toast({
                                    content: '登录失败，退出重试',
                                    type: 'error'
                                });
                            }
                        },
                        fail: function () {
                            wx.hideLoading();
                            $Toast({
                                content: '登录失败，退出重试',
                                type: 'error'
                            });
                        },
                    });
                }
            },
            fail: function (err) {
                console.log("获取失败: ", err);
                //用户按了拒绝按钮
                wx.showModal({
                    title: '温馨提示',
                    content: '您点击了拒绝授权，将无法获取用户标识，点击返回后将返回上一页，小程序提供的部分功能将受限。',
                    showCancel: false,
                    confirmText: '返回',
                    success: function (res) {
                        // 用户没有授权成功，不需要改变 isHide 的值
                        if (res.confirm) {
                            console.log('用户点击了“返回”');
                            wx.navigateBack();
                        }
                    }
                });
            }
        });
    },
});